package org.jeecg.modules.rooms.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.dto.BasePage;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.rooms.DTO.CesServiceTypeSearchDto;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.entity.CesServiceMaintenance;
import org.jeecg.modules.rooms.entity.CesServiceType;
import org.jeecg.modules.rooms.mapper.CesServiceTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 维修服务类型表 服务实现类
 * </p>
 *
 * @author fendo
 * @since 2023-03-16
 */
@Service
public class CesServiceTypeServiceImpl extends ServiceImpl<CesServiceTypeMapper, CesServiceType> implements IService<CesServiceType> {
    @Resource
    private CesServiceTypeMapper serviceTypeMapper;


    public Result fetch(CesServiceTypeSearchDto dto){
        QueryWrapper<CesServiceType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CesServiceType.HOTEL_ID,dto.getHotelId());
        queryWrapper.eq(CesServiceType.INVALID,false);
        IPage<CesServiceType> dataPage = serviceTypeMapper.selectPage(new Page<>(dto.getPageNo().intValue(),dto.getPageSize().intValue()),queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("records",dataPage.getRecords());
        map.put("total",dataPage.getTotal());
        return Result.OK(map);
    }

    /**
     * 创建
     * @param cesServiceType
     * @return
     */
    public Result create(CesServiceType cesServiceType){
        QueryWrapper<CesServiceType> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq(CesServiceType.NAME,cesServiceType.getName());
        queryWrapper.eq(CesServiceType.HOTEL_ID,cesServiceType.getHotelId());
        queryWrapper.eq(CesServiceType.INVALID,false);

        CesServiceType exit = serviceTypeMapper.selectOne(queryWrapper);
        if(!ObjectUtils.isEmpty(exit)) return Result.error("名称重复!");

        cesServiceType.setCreateAt(LocalDateTime.now());
        cesServiceType.setUpdateAt(LocalDateTime.now());
        cesServiceType.setInvalid(false);
        save(cesServiceType);
        return Result.ok("成功!");
    }



    /**
     * 修改
     * @param cesServiceType
     * @return
     */
    public Result modify(CesServiceType cesServiceType){
        CesServiceType type = serviceTypeMapper.selectOne(Wrappers.<CesServiceType>lambdaQuery().eq(CesServiceType::getId,cesServiceType.getId()));
        if(ObjectUtils.isEmpty(type)) return Result.error("数据未找到!");
        if(!type.getName().equals(type.getName())){
            QueryWrapper<CesServiceType> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq(CesServiceType.NAME,type.getName());
            queryWrapper.eq(CesServiceType.HOTEL_ID,type.getHotelId());
            queryWrapper.eq(CesServiceType.INVALID,false);
            queryWrapper.ne(CesServiceType.ID,type.getId());
            CesServiceType serviceType = serviceTypeMapper.selectOne(queryWrapper);
            if(!ObjectUtils.isEmpty(serviceType)) return Result.error("有重复名称的数据!");
        }
        cesServiceType.setUpdateAt(LocalDateTime.now());
        saveOrUpdate(cesServiceType);
        return Result.ok("成功!");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public Result delete(String id,String hotelId){
        CesServiceType cesServiceType = serviceTypeMapper.selectOne(Wrappers.<CesServiceType>lambdaQuery().eq(CesServiceType::getId,id).eq(CesServiceType::getHotelId,hotelId).eq(CesServiceType::getInvalid,false));
        if(ObjectUtils.isEmpty(cesServiceType)) return Result.error("数据未找到!");
        cesServiceType.setUpdateAt(LocalDateTime.now());
        cesServiceType.setInvalid(true);
        saveOrUpdate(cesServiceType);
        return Result.ok("删除成功!");
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    public Result deleteBatch(List<String> ids, String hotelId){
        List<CesServiceType> list = serviceTypeMapper.selectList(Wrappers.<CesServiceType>lambdaQuery().eq(CesServiceType::getInvalid,false).eq(CesServiceType::getHotelId,hotelId).in(CesServiceType::getId,ids));
        if(CollectionUtil.isEmpty(list)) return Result.ok("删除成功!");
        list.forEach(v -> {
            v.setUpdateAt(LocalDateTime.now());
            v.setInvalid(true);
        });

        saveOrUpdateBatch(list);
        return Result.ok("删除成功!");
    }


    public List<CesServiceType> fetchByIds(List<String> ids){
        if(CollectionUtil.isEmpty(ids)) return new ArrayList<>();
        return serviceTypeMapper.selectBatchIds(ids);
    }

}
